/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2015-11-09 11:49:40
 * Last Modified: 2016-01-20 15:47:43
 * File Name: controllers/accountbind.go
 * Description:
 */
package controllers

import (
	"encoding/json"

	"passport/account"
	"passport/captcha"
	"passport/errorcode"
	"passport/user"

	log "github.com/cihub/seelog"
)

type AccountBindController struct {
	LoginedController
}

/*
	绑定账号查询
*/
func (p *AccountBindController) QueryBindedAccount() {
	defer p.Output("QueryBindedAccount")

	accounts, err := account.GetAccountBindService().GetBindedAccountForUserid(p.UserId)
	if err != nil {
		log.Warnf("GetBindedAccountForUserid for uid: %s, error msg: %s", p.UserId, err.Error())
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INTERNAL_ERROR)
		return
	}

	data := make([]map[string]interface{}, 0, len(accounts))
	for extFrom, extId := range accounts {
		item := make(map[string]interface{})
		item["ext_from"] = extFrom
		item["ext_id"] = extId
		if ai, err := account.GetAccountService().GetAllAccountInfo(extFrom, extId); err == nil {
			if ai != nil {
				item["is_third"] = ai.ThirdAccount
				item["bind_time"] = ai.InsertTime
				item["nick_name"] = ai.NickName
				data = append(data, item)
			} else {
				log.Warnf("GetBindedAccountInfo empty for extFrom: %s, extId: %s", extFrom, extId)
			}
		}
	}
	p.Resp["code"] = errorcode.EC_GL_SUCCESS
	p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
	p.Resp["accounts"] = data
}

/*
	绑定关联第三方账号
*/
func (p *AccountBindController) BindThirdAccount() {
	defer p.Output("BindThirdAccount")

	body := p.Ctx.Input.RequestBody
	data := &account.AccountBindInfoReq{}
	if err := json.Unmarshal(body, data); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	if data.ExtFrom == "" || data.ExtId == "" {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	bindStatus, msg := account.GetAccountBindService().AccountBind(p.UserId, true, data)
	p.Resp["code"] = bindStatus
	p.Resp["msg"] = msg
}

/*
	解绑关联第三方账号
*/
func (p *AccountBindController) UnbindThirdAccount() {
	defer p.Output("UnbindThirdAccount")

	body := p.Ctx.Input.RequestBody
	data := &account.AccountBindInfoReq{}
	if err := json.Unmarshal(body, data); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	if data.ExtFrom == "" || data.ExtId == "" {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	unbindStatus, msg := account.GetAccountBindService().AccountUnbind(p.UserId, data.ExtFrom, data.ExtId)
	p.Resp["code"] = unbindStatus
	p.Resp["msg"] = msg
}

/*
	绑定手机号码
*/
func (p *AccountBindController) BindMobileAccount() {
	defer p.Output("BindMobileAccount")

	body := p.Ctx.Input.RequestBody
	data := &account.MobileAccountBindReq{}
	if err := json.Unmarshal(body, data); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	if data.Password == "" {
		log.Warnf("bind mobile account without password for mobile: %s", data.Mobile)
		p.Resp["code"] = errorcode.EC_ACCOUNT_BIND_MOBILE_WITHOUT_PASS
		p.Resp["msg"] = "missing password when bind account"
		return
	}

	captchaValidateReq := &captcha.ValidateCaptchaReq{
		Mobile:  data.Mobile,
		Bid:     captcha.CAPTCHA_MOBILE_BIND,
		Captcha: data.Captcha,
	}

	if code, msg := captcha.GetCaptchaService().ValidateCaptchaForMobile(captchaValidateReq); code != errorcode.EC_GL_SUCCESS {
		p.Resp["code"] = code
		p.Resp["msg"] = msg
		return
	}

	bindinfoReq := &account.AccountBindInfoReq{
		ExtFrom: account.ACCOUNT_TYPE_MOBILE,
		ExtId:   data.Mobile,
	}
	bindStatus, msg := account.GetAccountBindService().AccountBind(p.UserId, false, bindinfoReq)
	p.Resp["code"] = bindStatus
	p.Resp["msg"] = msg
	if bindStatus == errorcode.EC_GL_SUCCESS {
		captcha.GetCaptchaService().DeleteCaptchaInfoItem(data.Mobile, captcha.CAPTCHA_MOBILE_BIND)
		uinfochanger := user.NewUserInfoChanger(p.UserId)
		uinfochanger.SetPassword(data.Password)
		uinfochanger.SetMobile(data.Mobile)
		uinfochanger.SetIsThirdAccount(account.AI_SELF_ACCOUNT)
		uinfochanger.Save()
	}
}
